#ifndef __H_MVP_TRANSPORT_PPS_PROXY_H__
#define __H_MVP_TRANSPORT_PPS_PROXY_H__

#include <memory>
#include <thread>
#include <vector>

#include "common/macros.h"
#include "transport/pps/pps.h"

namespace motovis {
namespace transport {

template <typename MessageT>
class PpsReceiver;

class PpsProxy
{
public:
    using Callback = std::function<void(const pps::ReceiveData *)>;

    virtual ~PpsProxy();

    template <typename MessageT>
    bool publish(const std::string &channelName, const std::shared_ptr<MessageT> &msg);

    void subscribe(const std::string &channelName, Callback &cb);

private:
    void exec();

    std::vector<std::shared_ptr<pps::PPS>> m_pps;

    std::shared_ptr<std::thread> m_thread;

    DECLARE_SINGLETON(PpsProxy)
};

template <typename MessageT>
bool PpsProxy::publish(const std::string &channelName, const std::shared_ptr<MessageT> &msg)
{
    std::shared_ptr<pps::PPS> pps(new (std::nothrow) pps::PPS(channelName, false));
    m_pps.push_back(pps);

    return pps->publish(msg.get(), sizeof(MessageT));
}

} // namespace transport
} // namespace motovis

#endif // __H_MVP_TRANSPORT_PPS_PROXY_H__
